package com.abcdefg.handlerdemo.demo5;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
/**
 * //@ControllerAdvice
 * //@ResponseBody
 * 这两个注解合并起来就是@RestControllerAdvice
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler
    public Result handler(Exception e) {
        log.info("发生异常"+e.getMessage());
        return Result.fail("内部错误，请联系管理员");
    }

    @ExceptionHandler
    public Result handler2(NullPointerException e) {
        log.info("发生异常"+e.getMessage());
        return Result.fail("发生空指针异常，请联系管理员");
    }

    @ExceptionHandler
    public Result handler3(IndexOutOfBoundsException e) {
        log.info("发生异常"+e.getMessage());
        return Result.fail("发生数组下标越界，请联系管理员");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result handler4(Exception e) {
        log.info("发生异常"+e.getMessage());
        return Result.fail("发生数组下标越界，请联系管理员");
    }
}
